<?php
require $_SERVER['DOCUMENT_ROOT'].'/../_core/common.php';

//权限验证
if( Role::init() == false ){
	exit('没有权限');
}

$db = new DB('news');

$orders = isset($_POST['orders']) ? $_POST['orders'] : 0;
$title_id = isset($_POST['title_id']) ? $_POST['title_id'] : 0;
$category_id = isset($_GET['category_id']) ? intval($_GET['category_id']) : 0;

//检验必要参数
if($category_id<=0 || !is_array($orders) || !is_array($title_id) || count($orders)!=count($title_id) ){
	echo '<script>alert("参数有误！");window.history.go(-1);</script>';
	exit;
}

$orders_title_id_array = array();
foreach($title_id as $k=>$v){
	$orders_title_id_array[$v] = $orders[$k];
}

$title_id_string = '';
foreach($orders_title_id_array as $key=>$val){
	$data = array('orders'=>$val);
	$db->table('news')->where('title_id="'.$key.'"')->update($data)->run();
	
	$title_id_string .= $key.',';
}
$title_id_string = substr($title_id_string, 0, -1);

//记录动作日志
Log::logs_articles($category_id, 6, $title_id_string);

$error_title = '排序成功 - 更新排序';
$error_content = '排序成功！';
$error_pre_button = '<a href="/admin/news/news_lists.php?category_id='.$category_id.'" style="margin-right:20px;" id="pre_button">〈〈 返回列表</a>';
$error_next_button = '';
require template('admin/news/news_error');
exit;